package com.yuanfengerp.basicInfoManage.paymode.service.imlp;

import com.yuanfengerp.basicInfoManage.paymode.dao.IPayMethodInfoMapper;
import com.yuanfengerp.basicInfoManage.paymode.moudel.PayMethodInfo;
import com.yuanfengerp.basicInfoManage.paymode.service.IPayMethodService;
import com.yuanfengerp.basicInfoManage.warehouseinfo.model.Warehouseinfo;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.apache.ibatis.annotations.Param;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuanfengerp.publicUtil.Result;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class IPayMethodServiceImpl implements IPayMethodService {
    @Autowired
    private IPayMethodInfoMapper ipmim;
    SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);

    /**
     * 删除支付方式信息
     *
     * @param payment_methods_ordinal 支付方式的序号
     * @return 删除操作的结果
     */
    @Override
    public Result<String> deletePayMethodInfo(int payment_methods_ordinal) {
        try {
            int i = ipmim.deletePayMethodInfo(payment_methods_ordinal);
            return i > 0 ? Result.success("删除成功") : Result.failure(400, "删除失败");
        } catch (Exception e) {
            Logger logger = LoggerFactory.getLogger(this.getClass());
            logger.error("Error occurred while deleting pay method info", e);
            return Result.failure(500, "内部错误");
        }
    }

    /**
     * 插入支付方式信息
     *
     * @param payMethodInfo 要插入的支付方式信息对象
     * @return 插入操作的结果
     */
    @Override
    public Result<String> insertPayMethodInfo(PayMethodInfo payMethodInfo) {

            if (ipmim.selectPayMethodInfoByName(payMethodInfo.getPayment_methods_name()) != null)
                return Result.failure(400, "支付方式已存在");
            if (payMethodInfo.getPayment_methods_name() == null)
                return Result.failure(400, "支付方式名称不能为空");
            payMethodInfo.setPayment_methods_ID(idWorker.nextId());
            payMethodInfo.setPayment_methods_state(0);

            int i = ipmim.insertPayMethodInfo(payMethodInfo);
            return i > 0 ? Result.success("添加成功") : Result.failure(400, "添加失败");


    }

    /**
     * 查询所有支付方式信息
     *
     * @return 所有支付方式信息的列表
     */
    @Override
    public Result<List<PayMethodInfo>> selectAllPayMethodInfo(Map<String, Object> params) {
        // 获取 pageNum 和 pageSize，并提供默认值
        Integer pageNum = (Integer) params.getOrDefault("pageSize", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageNum", 10);

        // 计算起始页码
        int startPage = (pageNum - 1) * pageSize;
        params.put("pageNum", startPage);

        List<PayMethodInfo> pays = ipmim.selectPayMethodInfo(params);
        if (!pays.isEmpty()) {
            return Result.success(pays, ipmim.count());
        } else {
            return Result.failure(400, "没有这条信息");
        }
    }


    /**
     * 更新支付方式信息
     *
     * @param payMethodInfo 要更新的支付方式信息对象
     * @return 更新操作的结果
     */
    @Override
    public Result<String> updatePayMethodInfo(PayMethodInfo payMethodInfo) {
        try {
            int i = ipmim.updatePayMethodInfo(payMethodInfo);
            return i > 0 ? Result.success("修改成功") : Result.failure(400, "修改失败");
        } catch (Exception e) {
            Logger logger = LoggerFactory.getLogger(this.getClass());
            logger.error("Error occurred while updating pay method info", e);
            return Result.failure(500, "内部错误");
        }
    }


    /**
     * 根据条件分页查询支付方式信息的业务逻辑方法
     *
     * @param params 包含查询条件及分页参数的Map对象，其中应包含"pageNum"（页码）和"pageSize"（每页数量）等关键参数
     * @return Result<List < PayMethodInfo>> 返回操作结果，若查询到数据且参数合法则包含支付方式信息列表及成功消息，
     * 若未查询到数据或参数不合法则包含相应的错误码400及错误消息
     */
    @Override
    public Result<List<PayMethodInfo>> selectPayMethodInfo(Map<String, Object> params) {
        List<PayMethodInfo> payMethodInfos = ipmim.selectAllPayMethodInfo(params);
        if (!(payMethodInfos.isEmpty())){
            return Result.success(payMethodInfos, ipmim.count());
        }else {
            return Result.failure(400, "没有这条信息");
        }
    }
}
